<?php

namespace Core;

class Error
{

    protected bool $debug;

    public function __construct(bool $debug = true)
    {
        $this->debug = $debug;
    }

    public function error()
    {
        error_reporting(0); // 显示所有错误
        set_error_handler([$this, 'handle'], E_ALL | E_STRICT);
    }

    public function handle($code, $error, $file, $line)
    {
        $msg = "$error ($code) --> $file ($line)<br>";
        switch ($code) {
            case E_NOTICE:
                if ($this->debug) {
                    echo 'Notice:'.$msg;
                }
                break;
            default:
                // echo 111;
                if ($this->debug) {
                    echo 'Error'.$msg;
                } else {
                    # 使用日志保存错误
                    $file = 'log/'.date('Y_m_d').'.log';
                    error_log(date('[c]').$msg.PHP_EOL, 3, $file);
                }
                die();
                break;
        }
        // echo $file;
    }
}